package com.wing.productionPlan.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.StrUtil;
import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.productionPlan.model.entity.ProductionCategory;
import com.wing.productionPlan.model.request.ProductionCategoryForm;
import com.wing.productionPlan.model.response.ProductionCategoryVO;
import com.wing.productionPlan.service.ProductionCategoryService;
import com.wing.qualityTesting.model.request.ProcessQualityCategoryForm;
import com.wing.web.annotation.RequestObjectFieldIgnore;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author
 */
@Validated
@Api(value = "Admin-生产计划分类管理", tags = "#/productionPlan/productionCategory")
@RestController("productionCategoryController")
@RequestMapping("productionPlan/admin/productionCategory")
@RequiredArgsConstructor
public class ProductionCategoryController {

    private final ProductionCategoryService productionCategoryService;


    @ApiOperation(value = "查询所有生产计划分类", notes = "搜索")
    @GetMapping("/list")
    public PageResult<ProductionCategoryVO> list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit,
            @RequestParam(defaultValue = "create_time") String sort,
            @RequestParam(defaultValue = "asc") String order
            ,Boolean isDeleted
            ,String startCreateTime
            ,String endCreateTime
    ) {
        Page<ProductionCategory> pg = new Page<>(page, limit);
        Map paramMap = new HashMap();
        //排序
        paramMap.put("sort", StrUtil.toUnderlineCase(sort));
        paramMap.put("order", StrUtil.toUnderlineCase(order));
        paramMap.put("isDeleted", isDeleted);
        if (StringUtils.isNotBlank(startCreateTime)) {
            paramMap.put("startCreateTime", startCreateTime);
        }
        if (StringUtils.isNotBlank(endCreateTime)) {
            paramMap.put("endCreateTime", endCreateTime);
        }
        //搜索
        IPage<ProductionCategoryVO> list = productionCategoryService.pageList4app(pg, paramMap);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }


    @ApiOperation(value = "添加生产计划分类", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/add")
    public JsonResult add(@Validated(ProductionCategoryForm.Add.class) ProductionCategoryForm qualityTestingCategoryForm) {
        if (productionCategoryService.add(qualityTestingCategoryForm) > 0) {
            return JsonResult.success("添加成功");
        }
        return JsonResult.fail("添加失败");
    }


    @ApiOperation(value = "修改生产计划分类", notes = "修改")
    @PutMapping("/update")
    public JsonResult update(@Validated(ProductionCategoryForm.Update.class) ProductionCategoryForm qualityTestingCategoryForm) {
        ProductionCategory qualityTestingCategory = productionCategoryService.getById(qualityTestingCategoryForm.getId());
        if (qualityTestingCategory == null) {
            return JsonResult.fail("修改失败！");
        }
        CopyOptions copyOptions = CopyOptions.create()
                .setEditable(ProductionCategory.class)
                .setIgnoreError(true)
                .setIgnoreNullValue(true);
        BeanUtil.copyProperties(qualityTestingCategoryForm, qualityTestingCategory, copyOptions);
        if (productionCategoryService.updateById(qualityTestingCategory)) {
            return JsonResult.success("修改成功！");
        }
        return JsonResult.fail("修改失败！");
    }


    @ApiOperation(value = "删除生产计划分类", notes = "删除")
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long qualityTestingCategoryId) {
        if (productionCategoryService.removeById(qualityTestingCategoryId)) {
            return JsonResult.success("删除成功");
        }
        return JsonResult.fail("删除失败");
    }


    @ApiOperation(value = "启用生产计划分类", notes = "启用")
    @com.wing.common.annotation.RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/enable")
    public JsonResult enable(@Validated(ProductionCategoryForm.Add.class) ProductionCategoryForm productionCategoryForm) {
        if (productionCategoryService.enable(productionCategoryForm.getId()) >= 0) {
            return JsonResult.success("启用成功");
        }
        return JsonResult.fail("启用失败");
    }

    @ApiOperation(value = "停用生产计划分类", notes = "停用")
    @com.wing.common.annotation.RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/disable")
    public JsonResult disable(@Validated(ProductionCategoryForm.Add.class) ProductionCategoryForm productionCategoryForm) {
        if (productionCategoryService.disable(productionCategoryForm.getId()) >= 0) {
            return JsonResult.success("停用成功");
        }
        return JsonResult.fail("停用失败");
    }

    @ApiOperation(value = "获取生产计划分类-下拉树")
    @GetMapping("/getTreeData")
    public JsonResult getTreeData(Long productionCategoryId) {
        return productionCategoryService.getTreeData(productionCategoryId);
    }

}
